import 'package:flutter/material.dart';

class BaseBottomProvider extends ChangeNotifier {
  bool showTop = false;
  final controllerCaches = <int, ScrollController>{};
  double edge = 500; //滚动临界值
  ScrollController? currentController;

  setSrollController(int index, {ScrollController? controller, bool forceToTop = false}) {
    print('setSrollController === $index ${controller}');
    currentController?.removeListener(_offsetListener);
    controller ??= controllerCaches[index];
    if (controller == null) return;
    controllerCaches[index] = controller;
    if (forceToTop) {
      showTop = false;
    } else if (controller.hasClients) {
      showTop = controller.offset > edge;
    } else {
      showTop = false;
    }
    currentController = controller;
    currentController?.addListener(_offsetListener);
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      notifyListeners();
    });
  }

  _offsetListener() {
    final tempShowTop = (currentController?.offset ?? 0) > edge;
    if (tempShowTop != showTop) {
      showTop = tempShowTop;
      notifyListeners();
    }
  }

  scrollToTop() {
    if (currentController?.hasClients == true) {
      currentController?.jumpTo(0);
    }
  }
}

class HomeBottomProvider extends BaseBottomProvider {
  static final HomeBottomProvider _instance = HomeBottomProvider._();

  factory HomeBottomProvider() => _instance;

  HomeBottomProvider._();
}

class DarkwebBottomProvider extends BaseBottomProvider {
  static final DarkwebBottomProvider _instance = DarkwebBottomProvider._();

  factory DarkwebBottomProvider() => _instance;

  DarkwebBottomProvider._();
}

class CommunityBottomProvider extends BaseBottomProvider {
  static final CommunityBottomProvider _instance = CommunityBottomProvider._();

  factory CommunityBottomProvider() => _instance;

  CommunityBottomProvider._();
}

class CartoonBottomProvider extends BaseBottomProvider {
  static final CartoonBottomProvider _instance = CartoonBottomProvider._();

  factory CartoonBottomProvider() => _instance;

  int fatherIndex = 0;
  // int subIndex = 0;
  final subIndexCaches = <int, int>{};

  CartoonBottomProvider._();
  final scrollCache = <String, ScrollController>{};

  setShowTop(bool isTop) {
    showTop = isTop;
    notifyListeners();
  }

  setScrollControllerWith({int? fatherI, int? subI, ScrollController? controller}) {
    currentController?.removeListener(_offsetListener);
    final tempFI = fatherI ?? fatherIndex;
    final tempSI = subI ?? subIndexCaches[tempFI] ?? 0;
    final key = '${tempFI}_$tempSI';
    controller ??= scrollCache[key];

    /// 没有滚动控制器的话 直接置顶
    if (controller == null) {
      showTop = false;
      notifyListeners();
      return;
    }
    scrollCache[key] = controller;
    if (controller.hasClients) {
      showTop = controller.offset > edge;
    }
    currentController = controller;
    fatherIndex = tempFI;
    subIndexCaches[tempFI] = tempSI;
    currentController?.addListener(_offsetListener);
    if (controller.hasClients) {
      notifyListeners();
    }
  }

  @override
  scrollToTop() {
    return super.scrollToTop();
  }
}
